﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace FQY.Infrastructure.Core
{
    /// <summary>
    /// 图片转换
    /// </summary>
    public class ImageConvertHelper
    {
        #region Image和Base64转换
        public static Bitmap Base64ToImage(string base64string)
        {
            //过滤特殊字符 
            //string filterBase64string = base64string.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
            //if (filterBase64string.Length % 4 > 0)
            //{
            //    filterBase64string = filterBase64string.PadRight(filterBase64string.Length + 4 - filterBase64string.Length % 4, '=');
            //}
            base64string = base64string.Replace("%2B", "+");
     
            byte[] b = Convert.FromBase64String(base64string);
            MemoryStream ms = new MemoryStream(b);
            Bitmap bitmap = Image.FromStream(ms) as Bitmap;// new Bitmap(ms);
            return bitmap;
        }

        public static string ImageToBase64(Image imageSource)
        {
            string strbaser64 = "";
            try
            {
                Bitmap bmp = new Bitmap(imageSource);
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                strbaser64 = Convert.ToBase64String(arr);
            }
            catch (Exception)
            {
                throw new Exception("ImageToBase64 Something wrong during convert!");
            }
            return strbaser64;
        }
        #endregion

        #region Image和Byte转换
        /// <summary> 
        /// 图片转换成字节流 
        /// </summary> 
        /// <param name="img">要转换的Image对象</param> 
        /// <returns>转换后返回的字节流</returns> 
        public static byte[] ImageToByte(Image imageSource)
        {
            MemoryStream ms = new MemoryStream();
            byte[] imagedata = null;
            imageSource.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            imagedata = ms.GetBuffer();
            return imagedata;
        }
        /// <summary> 
        /// 字节流转换成图片 
        /// </summary> 
        /// <param name="byt">要转换的字节流</param> 
        /// <returns>转换得到的Image对象</returns> 
        public static Image ByteToImage(byte[] arrByte)
        {
            MemoryStream ms = new MemoryStream(arrByte);
            Image img = Image.FromStream(ms);
            return img;
        }
        #endregion

        #region Url路径转Image
        public static Image GetURLImage(string imageUrl)
        {
            Image img;
            WebRequest wreq = WebRequest.Create(imageUrl);
            using (HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse())
            {
                using (Stream s = wresp.GetResponseStream())
                {
                    img = Image.FromStream(s);
                }
            }
            return img;
        }
        public static string GetURLImageToBase64(string imageUrl)
        {
            Image img = GetURLImage(imageUrl);
            return ImageToBase64(img);
        }
        #endregion
    }
}
